﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceSquirrel
{
	public static class JukeBox
	{

		private static readonly Dictionary<string, object[]> SONG_LOOKUP = new Dictionary<string, object[]>() {
			// name : (file, should loop?)
			{ "title", new object[] { "tensebranches", true } },
			{ "intro", new object[] { "open_skies", true } },
			{ "overworld", new object[] { "open_skies", true } },
			{ "ship", new object[] { "spacemachine", true } },
			{ "credits", new object[] { "credits", true } },
			{ "password", new object[] { "fluffytales", true } },
			{ "volcano", new object[] { "lunacy", true } },
			{ "bike", new object[] { "nopressure", true } },
			{ "water", new object[] { "squirrels_can_swim", true } },
			{ "gravity", new object[] { "deadlyacorns", true } },
			{ "death", new object[] { "oopsdead", false } },
			{ "gameover", new object[] { "fluffytales", true } },
		};

		private static readonly Dictionary<string, string> SONG_BY_MAP = new Dictionary<string, string>() {
			{ "ship_1", "ship" },
	
			{ "main", "overworld" },
			{ "balloon_2_overland", "overworld" },
				{ "balloon_3_overland", "overworld" },
			{ "bird_overland", "overworld" },
			{ "volcano_2_overland", "overworld" },
			{ "volcano_3_overland", "overworld" },
	
			{ "bike_level", "bike" },
			{ "bird_link", "gravity" },
	
			{ "exit_water_phase_locked", "water" },
			{ "exit_water_phase_unlocked", "water" },
	
			{ "grav_ascent", "gravity" },
			{ "grav_core", "gravity" },
			{ "grav_descent", "gravity" },
			{ "grav_descent_secret", "gravity" },
			{ "grav_gate", "gravity" },
	
			{ "lava_gate_2", "volcano" },
			{ "lava_gate_2_active", "volcano" },
			{ "lava_gate_3", "volcano" },
			{ "lava_gate_3_active", "volcano" },
			{ "volcano_1_ascent", "volcano" },
			{ "volcano_1_core", "volcano" },
			{ "volcano_1_descent", "volcano" },
			{ "volcano_2_core", "volcano" },
			{ "volcano_2_lower_ascent", "volcano" },
			{ "volcano_2_lower_descent", "volcano" },
			{ "volcano_2_upper_ascent", "volcano" },
			{ "volcano_2_upper_descent", "volcano" },
			{ "volcano_2_upper_secret", "volcano" },
			{ "volcano_3_bottom", "volcano" },
			{ "volcano_3_core", "volcano" },
			{ "volcano_3_middle", "volcano" },
			{ "volcano_3_secret", "volcano" },
			{ "volcano_3_top", "volcano" },
	
			{ "water_gate_1_locked", "water" },
			{ "water_gate_1_unlocked", "water" },
	
			{ "rope_test", "gravity" },
	
			{ "birdfeeder", "gravity" },
	
			{ "section_3_gate", "gravity" },
			{ "section_3_gate_active", "gravity" },
			{ "wlink_A1", "gravity" },
			{ "wlink_A2", "gravity" },
			{ "wlink_A3", "gravity" },
			{ "wlink_A4", "gravity" },
	
			{ "wlink_B_nowater", "water" },
			{ "wlink_B_somewater", "water" },
			{ "wlink_B_allwater", "water" },
	
			{ "wlink_D1_nowater", "water" },
			{ "wlink_D1_water", "water" },
			{ "wlink_D2_nowater", "water" },
			{ "wlink_D2_water", "water" },
			{ "wlink_D3_nowater", "water" },
			{ "wlink_D3_water", "water" },
	
			{ "wlink_E1_nowater", "water" },
			{ "wlink_E1_water", "water" },
			{ "wlink_E2", "water" },
			{ "wlink_E3", "water" },
		};


		private static string currentSong = null;
		private static bool actuallyPlaying = false;

		public static void EnsureSong(string songId)
		{
			if (currentSong != songId)
			{
				if (currentSong != null && actuallyPlaying)
				{
					// pygame.mixer.music.stop()
				}

				if (SONG_LOOKUP.ContainsKey(songId))
				{
					object[] song = SONG_LOOKUP[songId];
					string file = (string)song[0];
					bool loop = (bool)song[1];
					string path = "music/" + file + ".ogg";
					// pygame.mixer.music.load(path)
					// pygame.mixer.music.play(loop ? -1 : 0)
					actuallyPlaying = true;
				}
				else
				{
					actuallyPlaying = false;
				}
				currentSong = songId;
			}
		}

		public static void PlaySongForLevelMaybe(string levelId)
		{
			if (SONG_BY_MAP.ContainsKey(levelId))
			{
				JukeBox.EnsureSong(SONG_BY_MAP[levelId]);
			}
		}

		public static void Pause()
		{

		}

		public static void Unpause()
		{

		}
	}
}
